package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.AddCartForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.IAddressService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * jss
 * create-date: 2019/7/23-16:37
 */

@Slf4j
@RestController
@RequestMapping("/shopping")
@Api(tags = "CartController", description = "购物车控制层")
public class CartController {
    @Reference(timeout=3000,check = false)
    ICartService cartService;
    @Reference(timeout=3000,check = false)
    IAddressService addressService;
    /**
     * 获得购物车列表
     */
    @Anoymous
    @GetMapping("/carts")
    public ResponseData showCart(HttpServletRequest request) {
//        String tokenStr = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//
//        JSONObject jsonObject = JSON.parseObject(tokenStr);
//        long uid = jsonObject.getLong("uid");

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        String token = CookieUtil.getCookieValue(request, "access_token");
        Long uid =  addressService.getUserIdByToken(token);
        cartListByIdRequest.setUserId(uid);
//        cartListByIdRequest.setUserId(85l);

        CartListByIdResponse cartList = cartService.getCartListById(cartListByIdRequest);

        if (!ShoppingRetCode.SUCCESS.getCode().equals(cartList.getCode())) {
            return new ResponseUtil<>().setErrorMsg(cartList.getMsg());
        }
        return new ResponseUtil<>().setData(cartList.getCartProductDtos());
    }

    /**
     * 向购物车中添加数据
     * @param addCartForm
     * @return
     */
    @PostMapping("/carts")
//    @Anoymous
    public ResponseData addCart(@RequestBody AddCartForm addCartForm){

        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(addCartForm.getUserId());
        addCartRequest.setItemId(addCartForm.getProductId());
        addCartRequest.setNum(addCartForm.getProductNum());

        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);

        if (addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(addCartResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
    }


    @PutMapping("/carts")
    @Anoymous
    public ResponseData updateCart(@RequestBody AddCartForm updateCartForm){

        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(updateCartForm.getUserId());
        updateCartNumRequest.setItemId(updateCartForm.getProductId());
        updateCartNumRequest.setNum(updateCartForm.getProductNum());
        updateCartNumRequest.setChecked(updateCartForm.getChecked());

        UpdateCartNumResponse updateCartNumResponse = cartService.updateCartNum(updateCartNumRequest);

        if (updateCartNumResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(updateCartNumResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }

    @DeleteMapping("carts/{uid}/{pid}")
//    @Anoymous
    public ResponseData deleteCart(@PathVariable long uid, @PathVariable long pid){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setItemId(pid);
        deleteCartItemRequest.setUserId(uid);

        DeleteCartItemResponse deleteCartItemResponse = cartService.deleteCartItem(deleteCartItemRequest);

        if(deleteCartItemResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(deleteCartItemResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
    }

    @PutMapping("items")
    public ResponseData selectAllItem(@RequestBody CheckAllItemRequest checkAllItemRequest){

        CheckAllItemResponse checkAllItemResponse = cartService.checkAllCartItem(checkAllItemRequest);

        if(checkAllItemResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(checkAllItemResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(checkAllItemResponse.getMsg());
    }

    @DeleteMapping("/items/{id}")
    public ResponseData deleteCheckedItem(@PathVariable long id){
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(id);
        DeleteCheckedItemResposne deleteCheckedItemResposne = cartService.deleteCheckedItem(deleteCheckedItemRequest);

        if(deleteCheckedItemResposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(deleteCheckedItemResposne.getMsg());
        }

        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }
}
